/**
  数据双向绑定，目前是通过传入data里定义val值进行绑定，可修改成通过单独定义字段进行绑定
  第一种方法需要维护一份数据key值等
  等二种方法需要维护多份
 */
import { props } from "./constant";
import AddressChoose from "@/components/addressChoose"; //导入省市区街道地址选择组件
export default {
  name: "TableForm",
  props,
  components: {
    AddressChoose,
  },
  data() {
    return {
      formData: [],
      collapseFormData: [],
      queryCont: false,
    };
  },
  created() {
    this.updateFormData();
  },
  methods: {
    updateFormData(list = this.data) {
      const parseFormData = JSON.parse(JSON.stringify(list));
      this.formData = parseFormData.splice(0, 8);
      this.collapseFormData = parseFormData;
    },
    // 表单 box class
    getFormItemClass(item) {
      const { type } = item;
      let classStr = "";
      switch (type) {
        case "textarea":
          classStr = "textarea-item";
          break;
        case "date":
          classStr = "date-item";
          break;
      }
      return `${classStr} query-block`;
    },
    // 通知父组件
    emitSearch() {
      this.$emit("onSearch", {
        val: {
          ...this.modelData,
        },
        formData: this.formData,
      });
    },

    chooseAddress(obj) {
      const { codeList, name } = obj;
      this.modelData[name] = codeList;
    },
    // 查询
    onSearch() {
      // 校验
      if (Object.keys(this.rules).length) {
        this.$refs[this.formRef].validate((valid) => {
          if (valid) {
            this.emitSearch();
          } else {
            console.log("error submit!!");
            return false;
          }
        });
      } else this.emitSearch();
    },
    // 重置
    onReset() {
      this.$refs[this.formRef].resetFields();

      // 清除地址选择框条件
      if (this.$refs.mychild) {
        this.$refs.mychild.forEach((item) => item.clearData());
      }

      this.$emit("onReset", {
        size: this.defaultSize,
        current: 1,
      });
    },
  },
  watch: {
    data(n) {
      this.updateFormData(n);
    },
  },
};
